package com.jiangyg.mall.order.controller;

import com.jiangyg.mall.authz.MemberInfo;
import com.jiangyg.mall.authz.MemberInfoHolder;
import com.jiangyg.mall.core.support.restful.Result;
import com.jiangyg.mall.order.dto.OrderReleaseDTO;
import com.jiangyg.mall.order.dto.OrderSubmitDTO;
import com.jiangyg.mall.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 类描述：订单控制器
 *
 * @author jiangyg
 * @version email@jiangyg.com
 * @date 2021-10-30 07:36:29
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    /**
     * 订单接口
     */
    private final OrderService orderService;

    @Autowired
    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }

    /**
     * 功能描述：订单确认页面显示数据
     *
     * @return 显示数据
     */
    @ResponseBody
    @GetMapping("/info")
    public Result<Object> info() throws Exception {
        // 获取当前登录会员ID
        final Long memberId = MemberInfoHolder.loginMemberId();
        // 获取订单确认页面显示数据
        return Result.ok(orderService.info(memberId));
    }

    /**
     * 功能描述：提交订单
     *
     * @return 显示数据
     */
    @ResponseBody
    @GetMapping("/submit")
    public Result<Object> submit(@RequestBody @Validated OrderSubmitDTO dto) throws Exception {
        // 获取当前登录会员信息
        final MemberInfo memberInfo = MemberInfoHolder.loginMemberInfo();
        // 提交订单
        final long memberId = Long.parseLong(memberInfo.getId());
        orderService.submit(dto, memberId, memberInfo.getUsername());
        return Result.ok();
    }

    /**
     * 功能描述：订单取消
     *
     * @return 结果
     */
    @ResponseBody
    @GetMapping("/cancel")
    public Result<Object> cancel(@RequestBody @Validated OrderReleaseDTO dto) {
        // 获取当前登录会员信息
        final MemberInfo memberInfo = MemberInfoHolder.loginMemberInfo();
        // 订单取消
        orderService.release(dto, Long.parseLong(memberInfo.getId()));
        return Result.ok();
    }

}
